<template>
  <page-header-wrapper :title="false">
    <a-card title="积分活动">
      <template #extra>
        <a-tooltip>
          <template #title> 数据导出</template>
          <a-button type="primary" icon="database" @click="handleExpert(queryParam)"></a-button>
        </a-tooltip>
      </template>
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="24">
              <a-form-item label="筛选">
                <a-input :value="queryParam.keyword" placeholder="请输入" />
              </a-form-item>
            </a-col>
            <a-col :md="(!advanced && 8) || 24" :sm="24">
              <span
                class="table-page-search-submitButtons"
                :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
              >
                <a-button type="primary" :loading="loading" @click="handleSearch(queryParam)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <div class="table-operator">
        <a-button type="primary" icon="plus" @click="handleCreate()">新增活动</a-button>
      </div>

      <a-table
        ref="table"
        size="default"
        rowKey="key"
        :loading="loading"
        :columns="columns"
        :data-source="loadData"
        :pagination="pagination"
        @change="handleTableChange"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
        <span slot="beginDate" slot-scope="text, record">
          <div v-if="record.beginDate">{{ record.beginDate }}</div>
          <div v-else>未开始</div>
        </span>
        <span slot="endDate" slot-scope="text, record">
          <div v-if="record.endDate">{{ record.endDate }}</div>
          <div v-else>未结束</div>
        </span>
        <span slot="status" slot-scope="text, record">
          <div v-if="record.status == '0'">已结束</div>
          <div v-if="record.status == '1'">正在进行</div>
          <div v-if="record.status == '2'">未开启</div>
        </span>
        <span slot="actType" slot-scope="text, record">
          <div v-if="record.actType == '0'">私密活动</div>
          <div v-if="record.actType == '1'">公开活动</div>
        </span>
        <span slot="action" slot-scope="text, record">
          <template>
            <a-tag color="#428bca" @click="handleDetail(record)">进入</a-tag>
          </template>
        </span>
      </a-table>
      <create-form
        ref="createModal"
        :visible="visible"
        :loading="confirmLoading"
        :model="mdl"
        @cancel="handleCancel"
        @ok="handleOk"
      />
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { getMemActivityListUsingPOST,addMemActivityUsingPOST,exportMemActivityListUsingPOST } from '@/api/api/huiyuanguanli.ts'
import CreateForm from './creatForm'
export default {
  name: 'MemberListScoreActivityIndex',
  components: {
    CreateForm,
  },
  data() {
    return {
      mdl: null,
      priceMdl: null,
      visible: false,
      priceVisible: false,
      value: '',
      confirmLoading: false,
      priceLoading: false,
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {
        pageNo: 1,
        pageSize: 10,
        queryCondition: '',
        status: '',
      },
      // 表头
      columns: [
        {
          title: '活动ID',
          dataIndex: 'id',
        },
        {
          title: '名称',
          dataIndex: 'name',
        },
        {
          title: '创建人',
          dataIndex: 'creator',
        },
        {
          title: '开始时间',
          dataIndex: 'beginDate',
          scopedSlots: { customRender: 'beginDate' },
        },
        {
          title: '结束时间',
          dataIndex: 'endDate',
          scopedSlots: { customRender: 'endDate' },
        },
        {
          title: '状态',
          dataIndex: 'status',
          scopedSlots: { customRender: 'status' },
        },
        {
          title: '类型',
          dataIndex: 'actType',
          scopedSlots: { customRender: 'actType' },
        },
        {
          title: '参与人次',
          dataIndex: 'joinNum',
        },
        {
          title: '送出积分',
          dataIndex: 'sendIntegral',
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '180px',
          scopedSlots: { customRender: 'action' },
        },
      ],
      loadData: [],
      loading: false,
      pagination: {},
    }
  },
  created() {
    this.handleSearch(this.queryParam)
  },
  methods: {
    handleExpert(val){
      exportMemActivityListUsingPOST({...val}).then((res) => {
          const blob = new Blob([res], {
            type: 'application/vnd.ms-excel;charset=utf-8',
          })
          const filename = '积分活动列表' + '.xlsx'
          // 将blob对象转为一个URL
          var blobURL = window.URL.createObjectURL(blob)
          // 创建一个a标签
          var tempLink = document.createElement('a')
          // 隐藏a标签
          tempLink.style.display = 'none'
          // 设置a标签的href属性为blob对象转化的URL
          tempLink.href = blobURL
          // 给a标签添加下载属性
          tempLink.setAttribute('download', filename)
          if (typeof tempLink.download === 'undefined') {
            tempLink.setAttribute('target', '_blank')
          }
          // 将a标签添加到body当中
          document.body.appendChild(tempLink)
          // 启动下载
          tempLink.click()
          // 下载完毕删除a标签
          document.body.removeChild(tempLink)
          window.URL.revokeObjectURL(blobURL)
          this.$message.success('导出成功~')
        })  
    },
    handleCreate(){
      this.mdl = null
      this.visible = true
    },
    handleDetail(record) {
      this.$router.push({ path: `/member/list/scoreactivity/activityDetail/${record.id}` })
    },
    handleOk(e) {
      const form = this.$refs.createModal.form
      this.confirmLoading = true
      form.validateFields((errors, values) => {
        if (!errors) {
          addMemActivityUsingPOST({ ...values,memId:this.$route.params.id,status:e.value,isRepet:values.isRepet?0:1,actType:values.actType?0:1})
            .then(() => {
              this.handleSearch(this.queryParam)
              this.$message.success('操作成功')
              this.visible = false
              this.confirmLoading = false
              // 重置表单数据
              form.resetFields()
            })
            .catch((error) => {
              this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
            })
          this.confirmLoading = false
          this.visible = false
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel() {
      this.visible = false
      const form = this.$refs.createModal.form
      form.resetFields() // 清理表单数据（可不做）
    },
    handleSearch(val) {
      this.loading = true
      getMemActivityListUsingPOST(val)
        .then((res) => {
          this.loadData = res.data.list
          this.loading = false
          this.pagination = { ...this.pagination, total: Number(res.data.total) }
        })
        .catch((error) => {
          this.loading = false
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
    handleTableChange(pagination, filters, sorter) {
    const pager = { ...this.pagination }
    pager.current = pagination.current
 pager.pageSize = pagination.pageSize
    this.pagination = pager
    this.handleSearch({
      sortField: sorter.field,
      sortOrder: sorter.order,
      ...filters,
      pageNo: pagination.current,
      pageSize: pagination.pageSize,
      queryCondition: this.queryParam.queryCondition,
      status: this.queryParam.status,
      date: this.queryParam.date,
    })
  },
  },
  
}
</script>
